<?php


/**
* Puree CMS
* http://www.molotsoft.ru
*
* @package Puree_CMS
* @subpackage Admin/Behaviors
* @author Ivan Chistyakov <ivan@molotsoft.ru>
* @version $Id: tab_schema.php 1728 2010-09-30 12:18:28Z zetruger@gmail.com $
* @link $HeadURL: http://puree-cms.googlecode.com/svn/trunk/plugins/admin/models/behaviors/tab_schema.php $
*/


class TabSchemaBehavior extends ModelBehavior {

/******************************************************************************/

var $settings = array();

/******************************************************************************/

var $tabSchema = array(
	'general' => array('icon' => 'page'),
	'image' => array('icon' => 'image'),
	'picture' => array('icon' => 'picture'),
	'photo' => array('icon' => 'photo'),
	'logo' => array('icon' => 'tux'),
	'file' => array('icon' => 'disk'),
	'audio' => array('icon' => 'music'),
	'video' => array('icon' => 'television'),
	'habtm' => array('icon' => 'anchor'),
	'attachment_images' => array('type' => 'attachment', 'model' => 'AttachmentImage', 'icon' => 'images'),
	'attachment_files' => array('type' => 'attachment', 'model' => 'AttachmentFile', 'icon' => 'disk_multiple'),
	'attachment_documents' => array('type' => 'attachment', 'model' => 'AttachmentDocument', 'icon' => 'page_word'),
	'attachment_pricelists' => array('type' => 'attachment', 'model' => 'AttachmentPricelist', 'icon' => 'page_excel'),
	'attachment_audio_files' => array('type' => 'attachment', 'model' => 'AttachmentAudioFile', 'icon' => 'music'),
	'attachment_video_files' => array('type' => 'attachment', 'model' => 'AttachmentVideoFile', 'icon' => 'television'),
	'attachment_links' => array('type' => 'attachment', 'model' => 'AttachmentLink', 'icon' => 'link'),
	'meta' => array('icon' => 'html'),
	'seo' => array('icon' => 'html'),
	'info' => array('icon' => 'information')
);

/******************************************************************************/

function setup(&$Model, $settings = array()) {
	$this->settings[$Model->name] = (array)$settings;
}

/******************************************************************************/

function _describeTab(&$Model, $tab_name, $tab_description = array(), $fill_options = FALSE, $id = NULL) {
	return $tab_description;
}

function describeTab(&$Model, $tab_name, $tab_description = array(), $fill_options = FALSE, $id = NULL) {
	$tab_description_default = array(
		'type' => 'default',
		'name' => $tab_name,
		'icon' => 'tab',
		'label' => __($tab_name . '_tab', TRUE),
		'params' => array()
	);
	$tab_description = array_merge($tab_description_default, $tab_description);
	// behavior
	if (isset($this->tabSchema[$tab_name])) {
		$tab_description = array_merge($tab_description, $this->tabSchema[$tab_name]);
	}
	$tab_description = array_merge($tab_description, $this->_describeTab($Model, $tab_name, $tab_description, $fill_options, $id));
	// model
	if (property_exists($Model, 'tabSchema') && isset($Model->tabSchema[$tab_name])) {
		$tab_description = array_merge($tab_description, $Model->tabSchema[$tab_name]);
	}
	if (method_exists($Model, '_describeTab')) {
		$tab_description = array_merge($tab_description, $Model->_describeTab($tab_name, $tab_description, $fill_options, $id));
	}
	// params
	if (is_string($tab_description['params'])) {
		parse_str($tab_description['params'], $params);
		$tab_description['params'] = is_array($params) ? $params : array();
	}
	return $tab_description;
}

/******************************************************************************/

function getTabSchema(&$Model, $fill_options = FALSE, $id = NULL) {
	$tab_schema = array();
	foreach ($Model->getFieldSchema($fill_options, $id) as $field_name => $field_description) {
		$tab_name = 'general';
		if (isset($field_description['input']['tab'])) {
			$tab_name = $field_description['input']['tab'];
		}
		if (!isset($tab_schema[$tab_name])) {
			$tab_description = array(
				'field_schema' => array()
			);
			$tab_schema[$tab_name] = $this->describeTab($Model, $tab_name, $tab_description, $fill_options, $id);
		}
		$tab_schema[$tab_name]['field_schema'][$field_name] = $field_description;
	}
	// attachments
	if (!empty($id) && $Model->idExists($id)) {
		foreach ($Model->hasMany as $assoc_name => $assoc_description) {
			if ((bool)preg_match('~^Attachment~i', $assoc_name)) {
				$tab_name = Inflector::tableize($assoc_name);
				if (!isset($tab_schema[$tab_name])) {
					$tab_description = array(
						'type' => 'attachment',
						'model' => $assoc_name,
						'foreign_key' => $id
					);
					$tab_schema[$tab_name] = $this->describeTab($Model, $tab_name, $tab_description, $fill_options, $id);
				}
			}
		}
	}
	return $tab_schema;
}

/******************************************************************************/

}


?>